﻿Imports DTO
Imports System.Data.Linq

Public Class KhoDao
    Public Shared Function LayBang() As Table(Of Kho)
        Dim db As New ThuVienDataContext()
        Return db.Khos
    End Function

    Public Shared Function LayDanhSach() As IEnumerable(Of Kho)
        Dim lst As IEnumerable(Of Kho)
        Dim db As New ThuVienDataContext()
        lst = From k In db.Khos Select k
        Return lst
    End Function

    Public Shared Function LayNganChua(ByVal masach As Integer) As Kho
        Dim nc As Kho
        Dim db As New ThuVienDataContext()
        nc = (From k In db.Khos Where k.MS = masach Select k).Single
        Return nc
    End Function

    Public Shared Sub Them(ByVal t As Kho)
        Dim db As New ThuVienDataContext()
        db.Khos.InsertOnSubmit(t)
    End Sub

    Public Shared Sub CapNhat_muontra(ByVal k As Kho)
        Dim db As New ThuVienDataContext()
        Dim kh = (From s In db.Khos Where s.MK = k.MK Select s).Single

        kh.Ton_kho = k.Tong_so
        kh.Ton_kho = k.Ton_kho

        db.SubmitChanges()
    End Sub

    Public Shared Sub CapNhat(ByVal k As Kho)
        Dim db As New ThuVienDataContext()
        Dim kh = (From s In db.Khos Where s.MK = k.MK Select s).Single

        Dim t = k.Tong_so - kh.Tong_so
        kh.Tong_so = k.Tong_so
        'Truong hop them sach vao kho
        If t > 0 Then
            kh.Ton_kho = kh.Ton_kho + t
        End If

        db.SubmitChanges()
    End Sub
End Class
